package com.education.platform.management.controller.sys;

import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.entity.SysUserDO;
import com.education.platform.dao.model.SysUserModel;
import com.education.platform.management.service.SysDictionaryService;
import com.education.platform.management.service.SysUserService;
import com.education.platform.management.vo.request.ResetPwdVO;
import com.education.platform.management.vo.request.SysUserQueryVO;
import com.education.platform.management.vo.request.SysUserUpdateVO;
import com.education.platform.management.vo.response.SysUserPageInfoVO;
import com.education.platform.management.vo.response.SysUserResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* describe: 后台用户
* @author : 
* @date: 2020-06-30 09:05:21.722
**/
@RestController
@Api("系统用户管理")
@RequestMapping("sysUser")
@Validated
public class SysUserController {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(SysUserController.class);
    
	@Autowired
    private SysUserService sysUserService;
	@Autowired
    private SysDictionaryService sysDictionaryService;
    /**
     * 分页查询
     * @param sysUserQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public SysUserPageInfoVO selectPageList(@RequestBody @Valid SysUserQueryVO sysUserQueryVO) {
        String info = String.format("The method name[selectPageList] params:%s", sysUserQueryVO.toString());
        LOGGER.info(info);
        SysUserModel sysUser = BeanUtil.convert(sysUserQueryVO, SysUserModel.class);

        Page<SysUserResponseVO> page = PageHelper.startPage(sysUserQueryVO.getPageNum(), sysUserQueryVO.getPageSize());
        List<SysUserModel> sysUserList = sysUserService.getByCondition(sysUser);
        SysUserPageInfoVO sysUserPageInfo = new SysUserPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), sysUserPageInfo);
        sysUserPageInfo.setPageNum(sysUserQueryVO.getPageNum());
        List<SysUserResponseVO> voList = BeanUtil.convert(sysUserList, SysUserResponseVO.class);
        sysUserPageInfo.setList(voList);

        return sysUserPageInfo;
    }


    /**
     *
     * @param
     * @return
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping("save")
    public void insertOrUpdate(@RequestBody @Valid SysUserUpdateVO updateVO) {
        SysUserDO sysUserDO = BeanUtil.convert(updateVO, SysUserDO.class);
        sysUserService.insertOrUpdate(sysUserDO, Arrays.asList(updateVO.getRoleId()));
    }

    /**
     *
     * @param
     * @return
     */
    @ApiOperation(value = "重置密码")
    @PostMapping("resetPassword")
    public void resetPassword(@RequestBody @Valid ResetPwdVO updateVO) {
        SysUserDO update  =new SysUserDO();
        update.setId(updateVO.getId());
        update.setPassWord("123456");
        sysUserService.updateById(update);
    }

    /**
     *
     * @param
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public void delete(@RequestBody List<Integer> ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<SysUserDO> updateList = new ArrayList<>(ids.size());
        ids.parallelStream().forEach(id->{
            SysUserDO su = new SysUserDO();
            su.setId(id);
            su.setDeleted(true);
            updateList.add(su);
        });
        sysUserService.updateInBatch(updateList);
    }



}
